﻿#include "login.h"
#include "ui_login.h"
#include<QFile>
#include<QDebug>
#include<QMessageBox>
#include<QtSql/QSqlDatabase>
#include<QtSql/QSqlQuery>

login::login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    QFile file("../login.css");
    if(file.open(QFile::ReadOnly)){
        QString str=file.readAll();
        setStyleSheet(str);
    }else{
        qDebug()<<"open login.css fail";
    }
}

login::~login()
{
    delete ui;
}

void login::on_loginBtn_clicked()
{
    if(!this->db.connectMysql()){
        QMessageBox::critical(this,"登录","数据库连接出错,登录失败!","确定");
        return;
    }
    QSqlQuery query(QString("select*from user where username='%1' and password='%2'")
                    .arg(this->ui->userEdit->text()).arg(this->ui->pwdEdit->text()));
    if(query.size()>0){
        this->menuContent.show();
        this->close();
    }else{
        QMessageBox::critical(this,"登录","用户名或密码错误!","确定");
    }
}

void login::on_cancelBtn_clicked()
{
    this->close();
}
